﻿using System;
using System.Linq;
using Xunit;
using Babelfish.HTML;
using System.Net;
using Babelfish.Extensions;
using Babelfish.JSON;
using Babelfish.XML;
using System.Text;

namespace Babelfish.Tests
{
    public class Class1
    {
        [Fact]
        public void TestJSON()
        {
            INodeDocument doc = new JSONDocument("{a:156,pelle:'apan karlsson',b:[1,2,{apa:0},{pelle:['dfaslfksdfjk']}]}");

            PrintValues(doc);


            
        }

        [Fact]
        public void TestTicnet()
        {
            var doc = new JSONDocument(new WebClient().DownloadString("http://www.ticnet.se/browseQueryHandler.php?language=sv&type=EventQuery&cat=10101&g=Festivaler&location=coun_all&rdc_select=ALL"));

            var numberOfEvents = doc.Find(node => node.Name == "numFound").First().Text;
            var events = doc.Find(node => node.Name == "docs").First().ChildNodes;

            Console.WriteLine("{0}", numberOfEvents);
            
            foreach (var e in events)
            {
                Console.WriteLine("{0}", e.ChildNodes["EventName"].Text);
                Console.WriteLine("{0}", e.ChildNodes["EventDate"].Text);  

                
            }
        }

        [Fact]
        public void TestTvNu()
        {
            var doc = new HTMLDocument(new WebClient().DownloadString("http://www.tv.nu"));

            var tables = doc.Find(node => node.Attributes["class"] == "tabla_container");

            foreach (var table in tables)
            {
                var channelName = table.Find(node => node.Name == "p").FirstOrDefault().ChildNodes["a"].Text;

                var programs = table.Find(node => node.Attributes["class"] == "prog_tabla").Find(node => node.Name == "li");


                Console.WriteLine("{0}", channelName);

                foreach(var program in programs)
                    Console.WriteLine("- {0} : {1}", program.Text.Trim(), program.ChildNodes[1].Text.Trim());
            }
        }

        [Fact]
        public void TestSF()
        {

            var client = new WebClient();
            
            var dates = new XmlDocument(client.DownloadString("http://www.sf.se/bio/Booking?cmd=getDates&theaterId=94"));

            var dateValues = dates.Find(node => node.Name == "date" && node.Attributes["dateValue"] != null);

            foreach (var dateValue in dateValues)
            {
                Console.WriteLine("::{0}::", dateValue.Text);

                var doc = new XmlDocument(new WebClient().DownloadString(string.Format("http://www.sf.se/bio/Program?xml=films&date={0}&theatre=94&type=filmlist", dateValue.Text)));

                var films = doc.Find(node => node.Name == "film" && node.Attributes["id"] != null);

                foreach (var film in films)
                {
                    Console.WriteLine("- {0}", film.Find(node => node.Name == "name").First().Text);
                }
            }
            
        }

        [Fact]
        public void TestDebaser()
        {

            var debaser = new XmlDocument(new WebClient().DownloadString("http://www.debaser.se/debaser/api/?method=getevents&venue=medis&from=20100101&to=20100201&format=xml"));
            var events = debaser.Find(node => node.Name == "event" && node.ChildNodes["event"] != null);
            foreach (var e in events)
                Console.WriteLine("{0} : {1}", e.ChildNodes["eventdate"].Text, e.ChildNodes["event"].Text);

        }

        [Fact]
        public void TestXML()
        {
            INodeDocument doc = new XmlDocument("<?xml version=\"1.0\" encoding=\"UTF-8\"?><data><pelle>apan karlsson<kalle>en kalle innuti en pelle</kalle><pelle id=\"5\">ännu en pelle innuti en pelle</pelle></pelle></data>");
            PrintValues(doc);
        }

        [Fact]
        public void TestHTML()
        {
            INodeDocument doc = new HTMLDocument("<html><head></head><body><p class=\"searchresult\">this is the search results</p>fksdjfhh</body></html>");

            //doc = new HTMLDocument(new WebClient().DownloadString("http://www.google.com"));
            PrintValues(doc);

            
        }


        private void PrintValues(INode doc)
        {
            foreach (INode node in doc.Find(node => node.Name == "a"))
                Console.WriteLine("{0} = {1}", node.Name, node.Text);

            foreach (INode node in doc.Find(node => node.Name == "pelle"))
                Console.WriteLine("{0} = {1}", node.Name, node.Text);

            foreach (INode node in doc.Find(node => node.Name == "pelle" && node.Attributes["id"] != null))
                Console.WriteLine("{0} = {1}", node.Name, node.Text);

            foreach (INode node in doc.Find(node => node.Attributes["class"] == "searchresult"))
                Console.WriteLine("{0} = {1}", node.Name, node.Text);
        }
    }
}
